package com.app.service;

import com.app.model.main.Focus;
import com.app.model.response.RetStatus;
import com.app.repository.interfaces.FocusMapper;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.Map;

/**
 * Campus com.app.service
 *
 * @author Lichaojie
 * @version 2018/5/8 21:41
 */
@Service
public class FocusService {

	@Autowired
	private ResponseService responseService;
	@Autowired
	private FocusMapper focusMapper;

	public RetStatus doFocus(String parameter){
		ObjectMapper objectMapper = new ObjectMapper();
		Map paraMap = null;
		try{
			paraMap = objectMapper.readValue(parameter,Map.class);
		}catch (IOException e){
			e.printStackTrace();
			return responseService.doResponse(204);
		}

		int from = (int)paraMap.get("from");
		int to = (int)paraMap.get("to");
		String time = (String)paraMap.get("time");

		Focus focus = focusMapper.selectByParam(from,to);

		int flag = 0;
		if (focus == null){
			focus = new Focus();
			focus.setFromId((long)from)
					.setToId((long)to)
					.setTime(time)
					.setFlag((byte)1);
			flag = focusMapper.insert(focus);
		}else {
			focus.setFlag((byte)1);
			flag = focusMapper.updateByPrimaryKeySelective(focus);
		}

		return responseService.doResponse(100);
	}

	public RetStatus cancelFocus(String parameter){
		ObjectMapper objectMapper = new ObjectMapper();
		Map paraMap = null;
		try{
			paraMap = objectMapper.readValue(parameter,Map.class);
		}catch (IOException e){
			e.printStackTrace();
			return responseService.doResponse(204);
		}

		int from = (int)paraMap.get("from");
		int to = (int)paraMap.get("to");
		String time = (String)paraMap.get("time");

		Focus focus = focusMapper.selectByParam(from,to);
		if(focus == null){
			return responseService.doResponse(200);
		}else {
			focus.setFlag((byte)0)
			.setTime(time);
			focusMapper.updateByPrimaryKeySelective(focus);
			return responseService.doResponse(100);
		}
	}




}
